package com.tencent.miniproject.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.miniproject.R;
import com.tencent.miniproject.adapter.FragmentAdapter;
import com.tencent.miniproject.base.BaseActivity;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.fragment.NeighborFriendsFragment;
import com.tencent.miniproject.fragment.UserBillFragment;
import com.tencent.miniproject.utils.ProgressDialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements TencentLocationListener {
    private static final String TAG = "MainActivity";
    @BindView(R.id.tab_layout_title)
    TabLayout mTabLayoutTitle;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.iv_add)
    ImageView mIvAdd;
    @BindView(R.id.fab)
    FloatingActionButton mFab;

    private List<Fragment> mFragmentList;
    private List<String> mTitleList;
    private NeighborFriendsFragment mNeighborFragment;
    private NeighborFriendsFragment mFriendsFragment;
    private UserBillFragment mUserBillFragment;     //用户账单，我的页面
    private FragmentAdapter mFragmentAdapter;
    //腾讯地图定位manager
    private TencentLocationManager mLocationManager;
    private Double mLongitude;
    private Double mLatitude;

    @Override
    protected int setLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        initLocation();
    }

    @Override
    protected void initView() {
        ProgressDialogUtil.showProgressDialog(this, "正在定位中...");
    }

    @Override
    protected void initListener() {
    }

    @OnClick({R.id.iv_add, R.id.fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                startActivityForResult(new Intent(this, PublishInfoActivity.class), Constant.REFRESH_PUBLISH_SUCCESS_FINISH);
                break;
            case R.id.fab:
                startActivity(new Intent(this, SearchActivity.class));
                break;
        }
    }

    private void initLocation() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.READ_PHONE_STATE,
            };

            if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(permissions, Constant.GET_POSITION);
            } else {
                setPositionListener();
            }
        } else {
            setPositionListener();
        }
    }

    private void setPositionListener() {
        TencentLocationRequest request = TencentLocationRequest.create();
        request.setRequestLevel(3);
        //间隔5s
        request.setInterval(5000);
        mLocationManager = TencentLocationManager.getInstance(this);
        mLocationManager.requestLocationUpdates(request, this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constant.GET_POSITION){
            initLocation();
        }
    }

    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
        Log.i(TAG, "onLocationChanged11: " + tencentLocation.getAddress() + " " + i + " " + s);
        if (i == STATUS_DISABLED) {
            mLocationManager.removeUpdates(this);
            mLongitude = mLocationManager.getLastKnownLocation().getLongitude();
            mLatitude = mLocationManager.getLastKnownLocation().getLatitude();
            ProgressDialogUtil.dismiss();
            initAllData();
        }
    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {
        Log.i(TAG, "onLocationChanged22: " + s + " " + s1);
    }

    private void initAllData() {
        mNeighborFragment = NeighborFriendsFragment.newInstance(Constant.FRAGMENT_NEIGHBOR, null, mLongitude, mLatitude);
        mFriendsFragment = NeighborFriendsFragment.newInstance(Constant.FRAGMENT_FRIENDS, null, mLongitude, mLatitude);
        mUserBillFragment = UserBillFragment.newInstance(Constant.FRAGMENT_MINE_BILL);
        mFragmentList = new ArrayList<>();
        mFragmentList.add(mNeighborFragment);
        mFragmentList.add(mFriendsFragment);
        mFragmentList.add(mUserBillFragment);
        mTitleList = new ArrayList<>();
        mTitleList.add("附近的人");
        mTitleList.add("好友");
        mTitleList.add("我的");

        mTabLayoutTitle.addTab(mTabLayoutTitle.newTab().setText(mTitleList.get(0)));
        mTabLayoutTitle.addTab(mTabLayoutTitle.newTab().setText(mTitleList.get(1)));
        mTabLayoutTitle.addTab(mTabLayoutTitle.newTab().setText(mTitleList.get(2)));
        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);
        mViewpager.setAdapter(mFragmentAdapter);
        mTabLayoutTitle.setupWithViewPager(mViewpager);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.GET_POSITION) {
            setPositionListener();
        }
        if (mUserBillFragment == null){
            return;
        }
        mUserBillFragment.onActivityResult(requestCode, resultCode, data);

    }

    public double getLongitude(){
        return mLongitude;
    }

    public double getLatitude(){
        return mLatitude;
    }
}
